package com.foreveredu.furedusys.service.impl;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.SpringSecurityMessageSource;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.transaction.annotation.Transactional;

import com.foreveredu.furedusys.persist.IRoleDao;
import com.foreveredu.furedusys.persist.IUserDao;
import com.foreveredu.furedusys.domain.Role;
import com.foreveredu.furedusys.domain.User;
import com.foreveredu.furedusys.service.ISystemService;

@Transactional
public class SystemService implements ISystemService {

	private IRoleDao roleDao = null;
	private IUserDao userDao = null;
	protected MessageSourceAccessor messages = SpringSecurityMessageSource.getAccessor();
	
	public IRoleDao getRoleDao() {
		return roleDao;
	}

	public void setRoleDao(IRoleDao roleDao) {
		this.roleDao = roleDao;
	}

	public IUserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	/*
	 * User对象的事务处理
	 */
	public int saveUser(User user) {
		return userDao.save(user);
	}

	public void deleteUserById(int id) {
		User user = findUserById(id);
		userDao.delete(user);
	}

	public void updateUser(User user) {
		userDao.update(user);
	}

	public List<User> findUsers() {
		return userDao.findAll();
	}

	public User findUserById(int id) {
		return userDao.findById(id);
	}

	public User findUserByUsername(String username) {
		return userDao.findUserByUsername(username);
	}

	public List<User> findUsersByRoleName(String roleName) {
		return roleDao.findUsersByRoleName(roleName);
	}

	@Override
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException, DataAccessException {
		User user = userDao.findUserByUsername(username);

		if (user == null) {
			throw new UsernameNotFoundException(messages.getMessage(
					"JdbcDaoImpl.notFound", new Object[] { username },
					"Username {0} not found"), username);
		}

		Set<Role> roles = user.getRoles();
		if (roles == null || roles.size() == 0) {
			throw new UsernameNotFoundException(messages.getMessage(
					"JdbcDaoImpl.notAuthority", new Object[] { username },
					"User {0} has no GrantedAuthority"), username);
		}

		Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
		for (Iterator<Role> iter = roles.iterator(); iter.hasNext();) {
			Role role = iter.next();
			GrantedAuthority authority = new GrantedAuthorityImpl("ROLE_" + role.getName());
			authorities.add(authority);
		}

		return new org.springframework.security.core.userdetails.User(
				user.getUsername(), user.getPassword(), true, true, true, true,
				authorities);
	}
	
	/*
	 * Role对象的事务处理
	 */
	public int saveRole(Role role) {
		return roleDao.save(role);
	}

	public void deleteRoleById(int id) {
		Role role = findRoleById(id);
		roleDao.delete(role);
	}

	public void updateRole(Role role) {
		roleDao.update(role);
	}

	public List<Role> findRoles() {
		return roleDao.findAll();
	}

	public Role findRoleById(int id) {
		return roleDao.findById(id);
	}

	public Role findRoleByName(String name) {
		return roleDao.findRoleByName(name);
	}

}
